package tk.hoopy.zedtown.input;

import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MouseInput implements MouseListener {
	
	private final long CLICK_SPEED = 200, DOUBLE_CLICK_SPEED = 500; //In milliseconds
	
	private long[] buttonClicked = {0,0,0};
	private Point[] clickLocation = new Point[3];
	private boolean[] newClick = {false,false,false};
	
	private long[] buttonPressed = {0,0,0};
	private Point[] pressLocation = new Point[3];
	private boolean[] newPress = {false,false,false};
	private boolean[] newRelease = {false,false,false};

	@Override
	public void mouseClicked(MouseEvent e) {
		buttonClicked[e.getButton()-1] = System.currentTimeMillis();
		clickLocation[e.getButton()-1] = e.getLocationOnScreen();
		newClick[e.getButton()-1] = true;
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		buttonPressed[e.getButton()-1] = System.currentTimeMillis();
		pressLocation[e.getButton()-1] = e.getLocationOnScreen();
		newPress[e.getButton()-1] = true;
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		buttonPressed[e.getButton()-1] = System.currentTimeMillis();
		pressLocation[e.getButton()-1] = e.getLocationOnScreen();
		newPress[e.getButton()-1] = false;
		newRelease[e.getButton()-1] = true;
	}
	
	public boolean newClickEvent(int mouseButton) {
		if(newClick[mouseButton-1]) {
			newClick[mouseButton-1] = false;
			return true;
		}
		return false;
	}
	
	public boolean newPressEvent(int mouseButton) {
		if(newPress[mouseButton-1]) {
			newPress[mouseButton-1] = false;
			return true;
		}
		return false;
	}
	
	public boolean newReleaseEvent(int mouseButton) {
		if(newRelease[mouseButton-1]) {
			newRelease[mouseButton-1] = false;
			return true;
		}
		return false;
	}
	
	public Point buttonPressed(int mouseButton) {
		if(System.currentTimeMillis() - buttonPressed[mouseButton-1] < DOUBLE_CLICK_SPEED) {
			return pressLocation[mouseButton-1];
		}
		return null;
	}
	
	public Point buttonClicked(int mouseButton) {
		if(System.currentTimeMillis() - buttonClicked[mouseButton-1] < CLICK_SPEED) {
			return clickLocation[mouseButton-1];
		}
		return null;
	}
	
	public int GetMousePositionX() {
		return (int) MouseInfo.getPointerInfo().getLocation().getX();
	}
	
	public int GetMousePositionY() {
		return (int) MouseInfo.getPointerInfo().getLocation().getY();
	}

}
